import NetQuest from "../net/NetQuest";
import UserData from "./UserData";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Launch extends cc.Component {

    @property(cc.Label)
    lbLoading: cc.Label = null;

    start() {
        this.enterGame();
    }
    private _enterAble = false;

    private isEnter = false;

    //进入游戏场景
    private enterGame() {

        //处理数据，然后再打开
        cc.director.preloadScene("Game",
            (data) => {
                if (this.isEnter) {
                    let rate = Math.trunc(data / 500);
                    rate = rate > 0.99 ? 0.99 : rate;
                    this.lbLoading.string = `加载中...${rate * 100}%`
                }
            },
            () => {
                //进入游戏
                let enterSceneCallback = () => {
                    cc.director.loadScene("Game");
                }
                //读档
                this.readUserData(enterSceneCallback)
            });
    }

    //读取存档
    private readUserData(callback = null) {
        let data = cc.sys.localStorage.getItem('userData');
        if (data) {
            UserData.getInstance().init(JSON.parse(data), callback);
        } else {
            callback && callback();
        }

    }
}
